import {app} from 'electron';
import {LogCategory, logger} from '../../logger';
import {BaseStoreService} from './BaseStoreService';
import {SettingsSchema} from '../types';

/**
 * 默认设置
 */
const defaultSettings: SettingsSchema = {
  theme: 'system',
  language: 'zh-CN',
  startPage: 'chrome://newtab',
  searchEngine: 'https://www.google.com/search?q={searchTerms}',
  downloadPath: app.getPath('downloads'),
  startOptions: {
    restoreTabs: true,
    startMinimized: false,
  },
  privacy: {
    clearDataOnExit: false,
    doNotTrack: false,
    blockThirdPartyCookies: false,
  }
};

/**
 * 设置存储服务类
 * 负责管理应用程序设置
 */
export class SettingsService extends BaseStoreService<SettingsSchema> {
  private static instance: SettingsService;

  /**
   * 私有构造函数
   */
  private constructor() {
    super('Settings', defaultSettings);
  }

  /**
   * 获取SettingsService单例实例
   */
  public static getInstance(): SettingsService {
    if (!SettingsService.instance) {
      SettingsService.instance = new SettingsService();
    }
    return SettingsService.instance;
  }

  /**
   * 获取应用设置
   */
  public getSettings(): SettingsSchema {
    logger.debug(this.moduleName, LogCategory.SYSTEM, '获取应用设置');
    return this.store.store;
  }

  /**
   * 更新应用设置
   * @param settings 要更新的设置项
   */
  public updateSettings(settings: Partial<SettingsSchema>): void {
    logger.info(this.moduleName, LogCategory.SYSTEM, '更新应用设置', settings);
    this.store.set(settings);
  }

  /**
   * 清理存储数据
   */
  public cleanupStore(): void {
    // 设置数据一般不需要清理
    logger.debug(this.moduleName, LogCategory.SYSTEM, '设置数据不需要清理');
  }
}
